package cn.demo.service.serviceimpl;

import cn.demo.Util.Page;
import cn.demo.dao.ProductDao;
import cn.demo.entity.Product;
import cn.demo.service.ProductService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author weizhongli
 * @Date 2020/4/5
 * @Version v1.0
 * @Description 商品管理业务层实现类
 **/
@Service
public class ProductServiceImpl implements ProductService {

    //依赖注入dao
    @Autowired
    private ProductDao productDao;


    /*
    * 查询所有商品
    * */
    public Page findAll(int PageIndex, int PageSize, String name, int providederId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("PageIndex", PageIndex * PageSize);
        map.put("PageSize", PageSize);
        map.put("name", name);
        map.put("providederId", providederId);
        Page page = new Page();
        page.setPageIndex(PageIndex);
        page.setPageSize(PageSize);
        int i = productDao.countProductList(name, providederId);
        page.setTotalRecords(i);
        int totalPages = page.getTotalRecords() % page.getPageSize() == 0 ? page.getTotalRecords() / page.getPageSize() : page.getTotalRecords() / page.getPageSize() + 1;
        page.setPageTotalpages(totalPages);
        page.setList(productDao.findAll(map));
        return page;

    }

    public int countProductList(@Param("name") String name, @Param("providederId") int providederId) {
        return productDao.countProductList(name, providederId);
    }

    /*
            * 根据id查询商品
            * */
    public Product findById(Integer id) {
        return productDao.findById(id);
    }

    /*
    * 根据名字查询商品
    * */
    public Product findByName(String name) {
        return productDao.findByName(name);
    }

    /*
    * 修改商品
    * */
    public int updateProduct(Product product) {
        return productDao.updateProduct(product);
    }

    /*
    * 添加商品
    * */
    public void addProduct(Product product) {
        productDao.addProduct(product);
    }

    /*
    * 删除商品
    * */
    public void deleteProduct(Integer id) {
        productDao.deleteProduct(id);
    }

    public List<Product> selectProductList() {
        return productDao.selectProductList();
    }

}
